<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "personinfo".
 *
 * @property string $stu_id
 * @property string $stu_name
 * @property string $password_hash
 * @property integer $sex
 * @property string $card_id
 * @property string $used_name
 * @property string $nation
 * @property string $political
 * @property string $address
 * @property string $religion
 * @property string $height
 * @property string $maritial_status
 * @property string $blood_type
 * @property string $telephone
 * @property string $qq
 * @property string $blank
 * @property string $blank_num
 * 
 */
class Personinfo extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'personinfo';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['stu_id','stu_name','sex','card_id','used_name','nation','political','address','religion','height','maritial_status','blood_type','telephone','qq','blank','blank_num'], 'required'],
        	[['stu_id'], 'unique'],
            [['sex'], 'integer'],
        		[['card_id'], 'string','length'=>18],
            [['stu_id', 'card_id', 'political'], 'string', 'max' => 20],
            [['stu_name', 'used_name', 'nation', 'blank'], 'string', 'max' => 50],
            [['password_hash'], 'string', 'max' => 60],
            ['address', 'string', 'max' => 200],
            [['religion'], 'string', 'max' => 100],
        		['height','string','length'=>3,'max'=>3],
        		['telephone','string','max'=>11,'length'=>11],
        		['qq','string','min'=>5,'max'=>15],
            [['maritial_status', 'blood_type'], 'string', 'max' => 10],
            [['blank_num'], 'string','min'=>16, 'max' => 19],
        ];
    }
    
    
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'stu_id' => '学号',
            'stu_name' => '姓名',
            'password_hash' => '密码',
            'sex' => '性别',
            'card_id' => '居民身份证号码',
            'used_name' => '曾用名',
            'nation' => '民族',
            'political' => '政治面貌',
            'address' => '籍贯',
            'religion' => '宗教',
            'height' => '身高',
            'maritial_status' => '婚姻状况',
            'blood_type' => '血型',
            'telephone' => '联系电话',
            'qq' => 'QQ号',
            'blank' => '银行',
            'blank_num' => '银行卡号',
           
        ];
    }

    /**
     * @return bool初始化密码
     */
    public function pwdtohash(){
    	$this->password_hash=\Yii::$app->getSecurity()->generatePasswordHash($this->stu_id);//学号默认初始密码
    	return true;
    }
    
    public function uppwdtohash(){
        //把原来刚刚修改的密码用hash加密
    	$this->password_hash=\Yii::$app->getSecurity()->generatePasswordHash($this->password_hash);
    	return true;
    }

    /**
     * 保存导入excel文件内容
     * @param $data  传入文件内容的数组
     * @return bool  保存成功返回true
     *
     */
    public static function SaveImport($data){

        try{


            foreach ($data as $value){
                $person=new Personinfo();
                $person->stu_id=$value['学号'].'';//学号要转化为字符型才能通过验证
                $person->stu_name=$value['姓名'].'';
                if($value['性别']=='男') //男0 女1
                {
                    $person->sex=0;
                }else{
                    $person->sex=1;
                }
                $person->card_id=str_replace("'","",$value['居民身份证号码']).'';//去除excel文件中身份证号码中的引号
                $person->used_name=$value['曾用名'].'';
                $person->nation=$value['民族'].'';
                $person-> political=$value['政治面貌'].'';
                $person->address=$value['籍贯'].'';
                $person->religion=$value['宗教'].'';
                $person->height=$value['身高'].'';
                $person-> maritial_status=$value['婚姻状况'].'';
                $person->blood_type=$value['血型'].'';
                $person->telephone=$value['联系电话'].'';
                $person->qq=$value['QQ号'].'';
                $person->blank=$value['银行'].'';
                $person->blank_num=str_replace("'","",$value['银行卡号']).'';////去除excel文件中银行卡号中的引号
                //初始化密码
               // $person->password_hash=\Yii::$app->getSecurity()->generatePasswordHash('123456');
                //初始密码是 “123456”
                $person->password_hash='$2y$13$6vrPypDJ17W0H6258V.XgOfMJMrWYD4MHOOyo/hbdhD.gDwjUhtMi';

                $person->save();
            }
            return true;

        }catch (\Exception $e){
            throw $e;
        }

    }
}
